<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jedrzej\Pimpable\PimpableTrait;

class Examination extends Model
{
    use HasFactory, PimpableTrait;

    //可以进行批量赋值的字段
    protected $fillable = [
        'id',
        'name',
        'type',
        'examination_mode',
        'subject_id',
        'category_id',
        'start_time',
        'over_time',
        'timer',
        'peoples',
        'user_id',
        'show_result',
        'show_answer',
        'mouse_out',
        'mouse_out_num',
        'reviewer',
        'test_list_id',
        'status'
    ];

    //类型转换
    protected $casts = [
        'peoples' => 'array',
        'created_at' => 'date:Y-m-d H:i:s',
        'updated_at' => 'date:Y-m-d H:i:s'
    ];

    //可搜索的字段
    protected $searchable = ['category_id', 'start_time', 'over_time', 'subject_id', 'status', 'name'];

    //可排序的字段
    protected $sortable = ['created_at,desc'];

    // 和科目的关联
    public function subject()
    {
        return $this->belongsTo(Category::class, 'subject_id', 'id');
    }

    // 和试卷的关联
    public function testList()
    {
        return $this->belongsTo(TestList::class, 'test_list_id', 'id');
    }

    // 与分类的关联
    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    // 与考试结果的关联
    public function result()
    {
        return $this->hasMany(StudentResult::class, 'examination_id', 'id');
    }

    // 与用户的关联
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
